﻿Imports System.IO.File
''Imports Twitterizer
Imports TwitterVB2
Public Class frmLogin
    Public tw As New TwitterAPI
    Dim consumer As String = "3rZoMe2v9ZDq5T1JoyVzyw"
    Dim consumersecret As String = "Za0raOo0mDs0xqgmmGxS1YI7hxUdoPmBnGtfFVHBek"
    Public oauthtoken As String
    Public oauthtokensecret As String
    Dim strPassword As String
    Dim strUserName As String
    Dim oFile As System.IO.File
    Dim oWrite As System.IO.StreamWriter


    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        '        txtUsername.Text = strUserName
        '       TextBox4.Text = strPassword
        '
        '        If My.Settings.m_strToken.Trim.Length = 0 Then
        '  Console.Write("Input Username: ")
        '  strUserName = Console.ReadLine()
        '  Console.Write("Input Password: ")
        '  strPassword = Console.ReadLine()
        ' tw.XAuth(strUserName, strPassword, consumer, consumersecret)
        ' My.Settings.m_strToken = tw.OAuth_Token
        ' My.Settings.m_strTokenSecret = tw.OAuth_TokenSecret
        ' My.Settings.Save()
        ' Else
        ' tw.AuthenticateWith(consumer, consumersecret, My.Settings.m_strToken, My.Settings.m_strTokenSecret)
        ' End If

        'If txtUsername.TextLength = 0 Then GoTo Skip

        Try
            Dim objReader As New System.IO.StreamReader(Application.StartupPath & "\" & txtUsername.Text.ToLower & "_OAuthtk.dat")
            My.Settings.m_strToken = objReader.ReadLine()
            My.Settings.Save()

            objReader.Close()

            Dim objReader1 As New System.IO.StreamReader(Application.StartupPath & "\" & txtUsername.Text.ToLower & "_OAuthtkST.dat")
            My.Settings.m_strTokenSecret = objReader1.ReadLine()
            My.Settings.Save()

            objReader1.Close()



            '            tw.AuthenticateWith(consumer, consumersecret, My.Settings.m_strToken, My.Settings.m_strTokenSecret)


            '            Form1.TextBox2.Select()

            '            Form1.Label2.Text = tw.AccountInformation.FollowersCount
            '            Form1.Label5.Text = tw.AccountInformation.FriendsCount
            '            Form1.Label7.Text = tw.AccountInformation.StatusesCount


            ' Form1.Text = "Twitter - " & tw.AccountInformation.ScreenName

            '   For Each entry In tw.HomeTimeline()

            '                Form1.ListView1.BeginUpdate()
            '                Dim n As New ListViewItem
            '                n = Form1.ListView1.Items.Add(entry.User.ScreenName.ToString)
            '                n.SubItems.Add(entry.Text.ToString)
            '
            '                n.SubItems.Add(entry.CreatedAt.ToString)
            '
            '
            '                Form1.ListView1.Refresh()
            '                Form1.ListView1.EndUpdate()
            '
            '        Next






            'NOT TO BE UNCOMMENTED!
            '  My.Computer.Network.DownloadFile(tw.AccountInformation.ProfileImageUrl, My.Application.Info.DirectoryPath + "/profilepic.png", "", "", False, 999999, True)
            '  Form1.PictureBox1.Image = Image.FromFile(My.Application.Info.DirectoryPath + "/profilepic.png")

            'If txtUsername.TextLength > 0 Then

            'My.Settings.LastUsername = txtUsername.Text
            'lstUsernames.Items.Add(txtUsername.Text)

            'ElseIf txtUsername.TextLength = 0 Then
            '


            'End If


            'My.Settings.lstUsernames.Items.Clear()

            'If lstUsernames.Items.Count > 0 Then

            'Dim i As Integer

            'For i = 0 To lstUsernames.Items.Count - 1

            'My.Settings.lstUsernames.Items.Add(My.Settings.lstUsernames.Items.Item(i))

            'Next

            'End If
            '

            'Dim where = My.Settings.lstUsernames.IndexOf(lstUsernames.SelectedItem)

            'frmMain.oauthtoken = My.Settings.lstToken.Item(where)
            'frmMain.oauthtokensecret = My.Settings.lstTokenSecret.Item(where)

            Me.Hide()
            frmMain.Show()
            'frmMain.strLogin = txtUsername.Text

            'oWrite = IO.File.CreateText(Application.StartupPath & "\" & txtUsername.Text.ToLower & "_OAuthtk.dat")
            'oWrite.WriteLine(My.Settings.m_strToken)
            'oWrite.Close()


            'oWrite = IO.File.CreateText(Application.StartupPath & "\" & txtUsername.Text.ToLower & "_OAuthtkST.dat")
            'oWrite.WriteLine(My.Settings.m_strTokenSecret)
            'oWrite.Close()

        Catch ex As Exception
            frmPIN.strUserName = txtUsername.Text
            Me.Hide()
            frmPIN.Show()
        End Try

Skip:
        'If txtUsername.TextLength = 0 Then

        'MsgBox("You must enter in a new login to use since you haven't had any previous logins.")

        'End If

        


        '    oWrite = IO.File.CreateText(Application.StartupPath & "\" & txtUsername.Text & "_OAuthtk.dat")
        '  oWrite.WriteLine("TuneYou! Activated")
        '  oWrite.WriteLine("DO NOT EDIT THIS FILE!") '1 = agree : 0 = disagree
        '  oWrite.Close()


    End Sub

    Private Sub txtUsername_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtUsername.KeyDown
        If e.KeyCode = Keys.Enter And txtUsername.TextLength > 0 Then
            Button4.PerformClick()
        End If
    End Sub

    Private Sub frmLogin_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        frmMain.Dispose()
        frmPIN.Dispose()
        End
    End Sub

    Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'If My.Settings.lstUsernames.Count > 0 Then

        'Dim i As Integer

        'For i = 0 To My.Settings.lstUsernames.Count - 1

        'lstUsernames.Items.Add(My.Settings.lstUsernames.Item(i))

        'Next

        'lstUsernames.FindStringExact(My.Settings.LastUsername)

        'End If

        txtUsername.Text = My.Settings.LastUsername

    End Sub

End Class